// 引入express模块
const express = require("express")
// 定义路由级中间件
const router = express.Router()
// 引入数据模型模块
const Users = require("../model/login")
// 引入jwt
const jwt = require("jsonwebtoken")

// // 登录API
router.post('/login', function (req, res) {
  const username = req.body.username;
  const password = req.body.password;
  Users.find({ username: username }, function (err, users) {
    if (users.length === 0) {
      res.send({ isSuccess: false, message: '该用户不存在' });
    } else if (users[0].password === password) {
      var obj = {
        ok: true
      }
      // 自定义密文
      var cip = "asd1sa32dacasasac"
      // 生成token
      var token = jwt.sign(obj, cip)
      res.send({ isSuccess: true, message: '登录成功', token });
    } else if (users[0].password !== password) {
      res.send({ isSuccess: false, message: '密码不正确，请重新输入' });
    }
  });
});

module.exports = router
